package com.jeesite.modules.banuser.users.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.banuser.users.entity.Users;
import com.jeesite.modules.banuser.users.service.UsersService;
import com.jeesite.modules.common.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * usersController
 * @author k
 * @version 2025-08-21
 */
@Controller
@RequestMapping(value = "${adminPath}/users/users")
public class UsersController extends BaseController {

	@Autowired
	private UsersService usersService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Users get(Long tid, boolean isNewRecord) {
		return usersService.get(tid, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	//@RequiresPermissions("users:users:view")
	@RequestMapping(value = {"list", ""})
	public String list(Users users, Model model) {
		model.addAttribute("users", users);
		return "modules/users/usersList";
	}
	
	/**
	 * 查询列表数据
	 */
	//@RequiresPermissions("users:users:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Users> listData(Users users, HttpServletRequest request, HttpServletResponse response) {
		users.setPage(new Page<>(request, response));
		Page<Users> page = usersService.findPage(users);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	//@RequiresPermissions("users:users:view")
	@RequestMapping(value = "form")
	public String form(Users users, Model model) {
		model.addAttribute("users", users);
		return "modules/users/usersForm";
	}

	/**
	 * 保存数据
	 */
	//RequiresPermissions("users:users:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Users users) {
		usersService.save(users);
		return renderResult(Global.TRUE, text("保存users成功！"));
	}
	
	/**
	 * 删除数据
	 */
	//@RequiresPermissions("users:users:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Users users) {
		usersService.delete(users);
		return renderResult(Global.TRUE, text("删除users成功！"));
	}
	/**
	 * 封禁用户
	 */
	//@RequiresPermissions("users:users:ban")
	@RequestMapping(value = "ban")
	@ResponseBody
	public Result<String> ban(Users users, String reason, Date unbanDate) {
		if (users ==null) return Result.success(renderResult(Global.TRUE, text("该用户不存在")));
		usersService.ban(users,reason,unbanDate);
		return  Result.success(renderResult(Global.TRUE, text("封禁users已提交！")));
	}
}